/*
Copyright (c) 2007 Emmanuel Bonnet, blog.emmanuelbonnet.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package wiiless.objects
{
	import flash.display.MovieClip;
	import wiiless.wiiless.WiilessList;
	import wiiless.wiiless.Wiiless;
	import wiiless.events.ObjectEvent;
	
	public class AObjectMove extends MovieClip
	{
		
		public static var SCALE_DEPTH = "depth";
		public static var SCALE_DOUBLE = "double";
		
		/**
		 * Id d'incrémentation pour chaque objet créé
		 */
		public static var idIncr:int = 0;
		
		/**
		 * Id de l'objet
		 */
		private var _iId:int;
		
		/**
		 * L'objet est rescalable (depth or double)
		 */
		public var scaleMode:String;
		
		/**
		 * Garder la proportion
		 */
		public var scaleProp:Boolean = true;
		
		/**
		 * L'object est déplacable
		 */
		public var autoMove:Boolean = true;
		
		/**
		 * Liste des Wiiless en hit
		 */
		private var _wiilessHit:WiilessList;
		
		/**
		 * Hauteur minimum
		 */
		public var minHeight:int = 100;
		
		/**
		 * Largeur minimum
		 */
		public var minWidth:int = 100;
		
		/**
		 * Profondeur
		 */
		public var _iZ:int = 10;
		
		/**
		 * Id de la wiiless autorisé à effectuer une action sur l'objet
		 */
		public var wiiless:int;
		
		/**
		 * Alpha over
		 */
		public var alphaOver:Number = .5;
		
		/**
		 * Si over
		 */
		private var _isOver:Boolean;
		
		/**
		 * Si en déplacement
		 */
		private var _isMove:Boolean;
		
		/**
		 * Si en scale depth
		 */
		private var _isScaleDepth:Boolean;
		
		/**
		 * Si en scale double
		 */
		private var _isScaleDouble:Boolean;
		
		/**
		 * Constructeur
		 */
		function AObjectMove()
		{
			_iId = ++AObjectMove.idIncr;
			_wiilessHit = new WiilessList();
		}
		
		/**
		 * Roll over d'une wiiless
		 */
		public function over():void
		{
			if(!_isOver)
			{	
				_isOver = true;
				alpha = alphaOver;
				var e:ObjectEvent = new ObjectEvent(ObjectEvent.ROLL_OVER);
				e.id = _iId;
				dispatchEvent(e);
			}
		}
		
		/**
		 * Roll out d'une wiiless
		 */
		public function out():void
		{
			if(_isOver)
			{
				_isOver = false;
				alpha = 1;
				var e:ObjectEvent = new ObjectEvent(ObjectEvent.ROLL_OUT);
				e.id = _iId;
				dispatchEvent(e);
			}
		}
		
		/**
		 * Déplacement de l'objet
		 */
		public function move():void
		{
			var e:ObjectEvent = new ObjectEvent(ObjectEvent.MOVE);
			e.id = _iId;
			e.point = {x:x, y:y, z:_iZ};
			dispatchEvent(e);
		}
		
		/**
		 * Scale de l'objet en fonction de la profondeur
		 */
		public function scaleDepth():void
		{
			var e:ObjectEvent = new ObjectEvent(ObjectEvent.SCALE_DEPTH);
			e.id = _iId;
			e.point = {x:x, y:y, z:_iZ};
			dispatchEvent(e);
		}
		
		/**
		 * Scale de l'objet en fonction de deux wiiless
		 */		
		public function scaleDouble():void
		{
			var e:ObjectEvent = new ObjectEvent(ObjectEvent.SCALE_DOUBLE);
			e.id = _iId;
			e.point = {x:x, y:y, z:_iZ};
			dispatchEvent(e);
		}
		
		/**
		 * 
		 */
		public function remove():void
		{
			var e:ObjectEvent = new ObjectEvent(ObjectEvent.REMOVE);
			e.id = _iId;
			e.point = {x:x, y:y, z:_iZ};
			dispatchEvent(e);
			parent.removeChild(this);
		}
		
		/**
		 * Ajoute le contact d'une wiiless
		 * 
		 * @param	pWiiless		Wiiless
		 */
		public function addWiilessHit(pWiiless:Wiiless):int
		{
			return _wiilessHit.addWiiless(pWiiless);
		}
		
		/**
		 * Supprime le contact d'une wiiless
		 * 
		 * @param	pWiiless		Wiiless
		 */
		public function removeWiilessHit(pWiiless:Wiiless):int
		{
			return _wiilessHit.removeWiiless(pWiiless);
		}
		
		/**
		 * Retourne la liste des wiiless en contact
		 */
		public function getWiilessHit():Array
		{
			return _wiilessHit.getList();
		}
		
		/**
		 * Id de l'objet
		 */
		public function get id():int
		{
			return _iId;
		}
		
		/**
		 * Profondeur de l'objet
		 */
		public function get z():int
		{
			return _iZ;
		}
		
		public function set z(pZ:int):void
		{
			_iZ = pZ;
			scaleX = scaleY = _iZ;
		}
		
	}
}